package com.sphd.emanu.activity.storagemanage;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.material.Storage;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class StorageManagePresenter extends BasePresenterImpl<StorageManageContract.View> implements StorageManageContract.Presenter {

    @Override
    public void selectStorage(int id) {
        Call<String> call = emanuService.selectStorage(id);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                List<Storage> storageList = GsonUtil.jsonToList(response.body(), Storage.class);
                mView.setStorageList(storageList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void addStorage(Integer sn, String name, String fullName, Long volume, int org) {
        emanuService.addStorage(sn, name, fullName, volume, org).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res = res(response);
                mView.setAddUpRes(res);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updateStorage(int id, Integer sn, String name, String fullName, Long volume, int org) {
        emanuService.updateStorage(id, sn, name, fullName, volume, org).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res = res(response);
                mView.setAddUpRes(res);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteStorage(int id, final Integer pos, final SweetAlertDialog sweetAlertDialog) {
        emanuService.deleteStorage(id).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.setDelRes(res(response), pos, sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
